<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2023/12/12
 * Time: 15:53
 */

namespace app\api\model;

class Address extends Base
{
    /**
     * 查询单条
     * @param $where
     * @param $field
     * @return array|bool|\PDOStatement|string|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    static function findAddress($where = [], $field = '*'){
        return self::where($where)->field($field)->find();
    }

    /**
     * 新增收货地址
     * @param $insert
     * @return int|string
     */
    static function insertAddress($insert = []){
        return self::insert($insert);
    }

    /**
     * 更新收货地址
     * @param $where
     * @param $update
     * @return Address
     */
    static function updateAddress($where = [], $update = []){
        return self::where($where)->update($update);
    }

    /**
     * 关联查询多条
     * @param $where
     * @param $field
     * @param $page
     * @return bool|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    static function selectJoinAddress($where = [], $field = '*', $page = 1){
        $data = self::where($where)
            ->join('up_area a1', 'a1.district_id = addr_sheng', 'LEFT')
            ->join('up_area a2', 'a2.district_id = addr_shi', 'LEFT')
            ->join('up_area a3', 'a3.district_id = addr_qu', 'LEFT')
            ->field($field)
            ->page($page, self::$limit)
            ->order('addr_is_default asc, addr_time desc')
            ->select();
        return $data;
    }

    /**
     * 统计数量
     * @param $where
     * @return int|string
     * @throws \think\Exception
     */
    static function countAddress($where = []){
        return self::where($where)->count();
    }

    /**
     * 关联查询单条
     * @param $where
     * @param $field
     * @return array|bool|float|int|mixed|object|\stdClass|null
     */
    static function findJoinAddress($where = [], $field = '*'){
        $data = self::where($where)
            ->join('up_area a1', 'a1.district_id = addr_sheng', 'LEFT')
            ->join('up_area a2', 'a2.district_id = addr_shi', 'LEFT')
            ->join('up_area a3', 'a3.district_id = addr_qu', 'LEFT')
            ->field($field)
            ->find();
        return $data;
    }
}